package com.EM1.www.servlet;

import com.EM1.www.dao.ConnectTeacher;
import com.EM1.www.dao.ConnectUser;
import com.EM1.www.po.Teacher;
import com.EM1.www.po.User;
import com.EM1.www.service.CheckTeacher;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/ChangeMsg")
public class ChangeMsg extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        String subject=request.getParameter("subject");
        String instruduction=request.getParameter("instruduction");
        String username=request.getParameter("username");
        CheckTeacher checkTeacher=new CheckTeacher();
        String warn = checkTeacher.checkTeacher(subject);
        if(warn!=null){
            request.setAttribute("warn",warn);
            request.getRequestDispatcher("changemsg.jsp").forward(request,response);
        }else {
            ConnectTeacher connectTeacher=new ConnectTeacher();
            ConnectUser connectUser=new ConnectUser();
            Teacher teacher=null;
            try {
                connectTeacher.changeMsg(username,subject,instruduction,user.getId());
                connectUser.update(username,user.getId());
                teacher=connectTeacher.selectTeacher(user.getId());
                user=connectUser.selectusername(username);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            session.setAttribute("teacher",teacher);
            session.setAttribute("user",user);
            request.getRequestDispatcher("teachermsg.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
